package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.WebConfig;
import com.hyk.dto.R;
import com.hyk.service.WebConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "资源配置控制器")
@RequestMapping("/webConfigs")
public class WebConfigController {

    @Resource
    private WebConfigService webConfigService;

    @GetMapping
    @ApiOperation("根据资源类型和名称查询资源配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "资源名称"),
            @ApiImplicitParam(name = "type",value = "资源类型"),
            @ApiImplicitParam(name = "current",value = "当前页数"),
            @ApiImplicitParam(name = "size",value = "每页大小")
    })
    @PreAuthorize("hasAuthority('web_config_query')")
    public R<Page<WebConfig>> list(@ApiIgnore Page<WebConfig> page,
                                   String name,String type){
        QueryWrapper<WebConfig> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("created");
        if (name != null && name != ""){
            wrapper.like("name",name);
        }
        if (type != null && type != ""){
            wrapper.eq("type",type);
        }
        return R.ok(webConfigService.page(page, wrapper));
    }

    @PostMapping
    @ApiOperation("添加资源配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "资源名称"),
            @ApiImplicitParam(name = "type",value = "资源类型"),
            @ApiImplicitParam(name = "sort",value = "排序"),
            @ApiImplicitParam(name = "status",value = "状态"),
            @ApiImplicitParam(name = "url",value = "url"),
            @ApiImplicitParam(name = "value",value = "图片链接")
    })
    @PreAuthorize("hasAuthority('web_config_create')")
    public R add(@RequestBody @Validated WebConfig webConfig){
        boolean save = webConfigService.save(webConfig);
        if (save) {
            return R.ok("新增成功！");
        }
        return R.fail("新增失败");
    }

    @PatchMapping
    @ApiOperation("编辑资源配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "created",value = "创建时间"),
            @ApiImplicitParam(name = "name",value = "资源名称"),
            @ApiImplicitParam(name = "type",value = "资源类型"),
            @ApiImplicitParam(name = "sort",value = "排序"),
            @ApiImplicitParam(name = "status",value = "状态"),
            @ApiImplicitParam(name = "url",value = "url"),
            @ApiImplicitParam(name = "value",value = "图片链接")
    })
    @PreAuthorize("hasAuthority('web_config_update')")
    public R update(@RequestBody @Validated WebConfig webConfig){
        webConfig.setCreated(new Date());
        boolean update = webConfigService.updateById(webConfig);
        if (update) {
            return R.ok("修改成功！");
        }
        return R.fail("修改失败");
    }

    @PostMapping("/delete")
    @ApiOperation("删除资源配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "id数组")
    })
    @PreAuthorize("hasAuthority('web_config_delete')")
    public R delete(@RequestBody String[] ids){
        if(ids==null || ids.length==0){
            return R.fail("请选择需要删除的资源配置！！！") ;
        }
        boolean delete = webConfigService.removeByIds(Arrays.asList(ids));
        if (delete) {
            return R.ok("删除成功！");
        }
        return R.fail("删除失败");
    }

    @GetMapping("/banners")
    public R<List<WebConfig>> banners(){
        List<WebConfig> list = webConfigService.list(new LambdaQueryWrapper<WebConfig>().
                eq(WebConfig::getStatus,1).
                eq(WebConfig::getType,"WEB_BANNER").
                orderByDesc(WebConfig::getSort));
        return R.ok(list);
    }

}
